/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.upn.sigemfi.vfi.bc;

import edu.upn.sigemfi.plt.bc.TarjetaDebitoFacadeLocal;
import edu.upn.sigemfi.plt.be.TarjetaDebito;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 *
 * @author Alex
 */
public class TarjetaDebitoDelegate {

     private TarjetaDebitoFacadeLocal local;

    public TarjetaDebitoDelegate() {
        local = lookupTarjetaDebitoFacade();
    }

    public void crear(TarjetaDebito tarjetaDebito) throws Exception {
        try {
            local.create(tarjetaDebito);
        } catch (Exception e) {
            local = lookupTarjetaDebitoFacade();
            throw new Exception("Crear Tarjeta Debito. " + e.getMessage(), e);
        }
    }

    public void editar(TarjetaDebito tarjetaDebito) throws Exception {
        try {
            local.edit(tarjetaDebito);
        } catch (Exception e) {
            local = lookupTarjetaDebitoFacade();
            throw new Exception("Editar Tarjeta Debito. " + e.getMessage(), e);
        }
    }

    public void eliminar(TarjetaDebito tarjetaDebito) throws Exception {
        try {
            local.remove(tarjetaDebito);
        } catch (Exception e) {
            local = lookupTarjetaDebitoFacade();
            throw new Exception("Eliminar Tarjeta Debito. " + e.getMessage(), e);
        }
    }

    public TarjetaDebito buscar(Long codigo) throws Exception {
        try {
            return local.find(codigo);
        } catch (Exception e) {
            local = lookupTarjetaDebitoFacade();
            throw new Exception("Buscar Tarjeta Debito. " + e.getMessage(), e);
        }
    }

    public List<TarjetaDebito> listar() throws Exception {
        try {
            return local.findAll();
        } catch (Exception e) {
            local = lookupTarjetaDebitoFacade();
            throw new Exception("Listar Tarjeta Debito. " + e.getMessage(), e);
        }
    }

     public TarjetaDebito buscarTarjetaPorNroTarjeta(String nroTarjeta, String clave) throws Exception {
        try {

            return local.buscarTarjetaPorNroTarjetayClave(nroTarjeta, clave);
        } catch (Exception e) {
            local = lookupTarjetaDebitoFacade();
            throw new Exception("Listar Tarjeta Debito. " + e.getMessage(), e);
        }
    }

    private TarjetaDebitoFacadeLocal lookupTarjetaDebitoFacade() {
        try {
            Context c = new InitialContext();
            return (TarjetaDebitoFacadeLocal) c.lookup("java:comp/env/TarjetaDebitoFacade");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }





}
